<template>
  <a-modal
    title="编辑任务下发"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          label="所属网关"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select
            :showSearch="true"
            mode="multiple"
            optionFilterProp="children"
            style="width: 100%"
            placeholder="请选择所属网关"
            v-decorator="['gatewayId', {rules: [{ required: true, message: '请选择所属网关' }]}]" >
            <a-select-option v-for="(item,index) in gatewayList" :key="index" :value="item.id" >{{ item.gatewayName }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="任务类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select
            :showSearch="true"
            optionFilterProp="children"
            style="width: 100%"
            placeholder="请选择任务类型"
            v-decorator="['taskType', {rules: [{ required: true, message: '请选择任务类型' }]}]" >
            <a-select-option v-for="(item,index) in gatewayTaskTypeList" :key="index" :value="item.code" >{{ item.value }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form></a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/gatewaytask/gatewaytask'
  import { list } from '@/api/modular/main/bizstation/bizStationManage'
  import { gatewayTypelist } from '@/api/modular/main/bizgatewaytype/bizGatewayTypeManage'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
  import { gatewayList } from '@/api/modular/main/bizgateway/bizGatewayManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        stationTree: [],
        gatewayTypeList: [],
        treeLoading: true,
        gatewayTaskStatusList: [],
        gatewayTaskTypeList: [],
        gatewayList: []
      }
    },
    created () {
      this.getGatewayTaskStatusList()
      this.getGatewayTaskTypeList()
    },
    methods: {
      getGatewayTaskStatusList () {
        sysDictTypeDropDown({ code: 'gateway_task_status' }).then((res) => {
          if (res.success) {
            this.gatewayTaskStatusList = res.data
          }
        })
      },
      getGatewayTaskTypeList () {
        sysDictTypeDropDown({ code: 'gateway_task_type' }).then((res) => {
          if (res.success) {
            const typeList = []
            res.data.forEach(row => {
              if (row.code !== '2' && row.code !== '5' && row.code !== '6' && row.code !== '7') {
                typeList.push(row)
              }
            })
            this.gatewayTaskTypeList = typeList
          }
        })
      },
      edit (record, stationId) {
        this.visible = true
        this.getStationTree()
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id,
            gatewayId: record.gatewayId,
            taskType: record.taskType
          })
        }, 100)
        gatewayList({ stationId: stationId }).then(res => {
          this.gatewayList = res.data
        })
      },
      getStationTree () {
        list().then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.stationTree = res.data
        })
        gatewayTypelist().then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.gatewayTypeList = res.data
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
